//
// Created by 11510 on 2024/6/18.
//
#include <iostream>

using namespace std;

class Base {
public:
    virtual void print() {
        cout << "Base::print()" << endl;
    }

    void display() {
        cout << "Base::display()" << endl;
    }

    virtual void show() {
        cout << "Base::show()" << endl;
    }

private:
    long _base = 10;
};

class Derived : public Base {
public:
    /* 覆盖的顺序是按基类的顺序 */
    virtual void print() {
        cout << "Derived::print()" << endl;
    }

    virtual void show() {
        cout << "Derived::show()" << endl;
    }

    void display() {
        cout << "Derived::display()" << endl;
    }

//private:
public:
    long _derived = 100;
};

void test0() {
    Derived d;
    long *pDerived = reinterpret_cast<long *>(&d);
    printf("%p, %d, %d\n", pDerived[0], pDerived[1], pDerived[2]);


    long *pVtable = reinterpret_cast<long *>(pDerived[0]);
    printf("%p, %p, %p\n", pVtable[0], pVtable[1], pVtable[2]);

    typedef void (*Function)();
    Function f = (Function) (pVtable[0]);
    f();
    f = (Function) (pVtable[1]);
    f();
    f = (Function) (pVtable[2]);
    f();
}

class Test{
public:
    virtual void func(){
        cout << "hello" << endl;
    }
};
void test1(){
    const char* str = "hello";
    Test test;
    long *ptr = (long*)(&test);
    long *function = (long*)(*ptr);
    printf("%p, %p\n", function, str);
}

void test2(){
    Base* base = new Derived();
    base->display();
    base->print();
//    base->_derived;/* 指针大小限制，访问不到子类私有 */

//    printf("%ld, %p\n", sizeof(*base), base);
//
//    long* pvtable = (long*)base;
//    long* vtable = (long*)pvtable[0];
//    typedef void (*Function)();
//    Function f = (Function) (vtable[0]);
//    f();


    delete base;
}

int main() {
    test2();
}